import 'package:flutter_cate/core/model/category_model.dart';
import 'package:flutter_cate/core/model/meal_model.dart';
import 'package:flutter_cate/core/viewmodel/meal_view_model.dart';
import 'package:flutter_cate/core/services/meal_request.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:collection/collection.dart';
import 'package:flutter_cate/ui/widgets/meal_item.dart';

// 方式二
class AMMealContent extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final category = ModalRoute.of(context).settings.arguments as AMCategoryModel;
    return Selector<AMMealViewModel, List<AMMealModel>>(
        builder: (ctx, meals, child) {
          return ListView.builder(
              itemCount: meals.length,
              itemBuilder: (ctx , index) {
                return AMMealItem(meals[index]);
              });
    }, selector: (ctx, mealVM) {
          return mealVM.meals.where((meal) => meal.categories.contains(category.id)).toList();
    }, shouldRebuild: (prev, next) => !ListEquality().equals(prev, next)
    );
  }
}




// 方式一
// class AMMealContent extends StatelessWidget {
//   @override
//   Widget build(BuildContext context) {
//     final category = ModalRoute.of(context).settings.arguments as AMCategoryModel;
//     return Consumer<AMMealViewModel>(builder: (ctx, mealVM, child) {
//       final meals = mealVM.meals.where((meal) => meal.categories.contains(category.id)).toList();
//       return ListView.builder(
//         itemBuilder: (ctx ,index) {
//           return ListTile(
//             title: Text(meals[index].title),
//           );
//         },
//         itemCount: meals.length,
//       );
//     });
//   }
// }
